package com.zhixue.content.api;

import com.zhixue.base.exception.ValidationGroups;
import com.zhixue.content.model.dto.AddCourseDto;
import com.zhixue.content.model.dto.CourseBaseInfoDto;
import com.zhixue.content.model.dto.EditCourseDto;
import com.zhixue.content.model.dto.QueryCourseParamsDto;
import com.zhixue.content.model.po.CourseBase;
import com.zhixue.base.model.PageParams;
import com.zhixue.base.model.PageResult;
import com.zhixue.content.service.CourseBaseInfoService;
import com.zhixue.content.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author Dragon
 * @description
 * @date 2023/9/16 10:55
 */
@Api(value = "课程信息编辑接口",tags = "课程信息编辑接口")
@RestController
public class CourseBaseInfoController {


    @Autowired
    private CourseBaseInfoService courseBaseInfoService;

    @ApiOperation("全部课程查询接口")
    @PreAuthorize("hasAuthority('zx_teachmanager_course_list')")
    @PostMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams,
                                       @RequestBody QueryCourseParamsDto queryCourseParams){

        SecurityUtil.ZxUser user = SecurityUtil.getUser();
        Long companyId = null;
        if (StringUtils.isNotEmpty(user.getCompanyId())) {
            companyId = Long.parseLong(user.getCompanyId());
        }
        PageResult<CourseBase> courseBasePageResult =courseBaseInfoService.queryCourseBaseList(companyId ,pageParams,queryCourseParams);

        return courseBasePageResult;
    }


    @ApiOperation("新增课程基础信息")
    @PostMapping("/course")
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated(ValidationGroups.Insert.class) AddCourseDto addCourseDto){
        //如果这里给@Validated设置了分组，那么JSR303只有表明组别的校验可以生效，但实际这个在前端有好多都已经做了拦截
        //机构id，由于认证系统没有上线暂时硬编码
        //Long companyId = 1232141425L;
        SecurityUtil.ZxUser user = SecurityUtil.getUser();
        Long companyId = null;
        if (StringUtils.isNotEmpty(user.getCompanyId())) {
            companyId = Long.parseLong(user.getCompanyId());
        }
        return courseBaseInfoService.createCourseBase(companyId,addCourseDto);
    }


    @ApiOperation("根据课程ID查询课程基础信息")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable Long courseId){
        SecurityUtil.ZxUser user = SecurityUtil.getUser();
        System.out.println("当前用户身份为：" + user);
        return courseBaseInfoService.getCourseBaseInfo(courseId);
    }


    @ApiOperation("修改课程基础信息接口")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated(ValidationGroups.Update.class) EditCourseDto editCourseDto) {

        //Long companyId = 1232141425L;
        SecurityUtil.ZxUser user = SecurityUtil.getUser();
        Long companyId = null;
        if (StringUtils.isNotEmpty(user.getCompanyId())) {
            companyId = Long.parseLong(user.getCompanyId());
        }
        return courseBaseInfoService.updateCourseBase(companyId, editCourseDto);
    }

    @ApiOperation("删除课程")
    @DeleteMapping("/course/{courseId}")
    public void deleteCourse(@PathVariable Long courseId) {

        //Long companyId = 1232141425L;
        SecurityUtil.ZxUser user = SecurityUtil.getUser();
        Long companyId = null;
        if (StringUtils.isNotEmpty(user.getCompanyId())) {
            companyId = Long.parseLong(user.getCompanyId());
        }
        courseBaseInfoService.delectCourse(companyId, courseId);
    }

}
